package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    //注入本地MemberDao
    @Autowired
    private MemberDao memberDao;
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        //判断用户是否输入了密码
        if (member.getPassword() != null) {
            //调用工具类给用户密码进行加密处理
            String md5 = MD5Utils.md5(member.getPassword());
            member.setPassword(md5);
        }
        memberDao.add(member);
    }

    //根据日期查询会员数量
    public List<Integer> findMemberCountBeforeDate(List<String> months) {
        //创建集合用于存储会员数量
        List<Integer> list = new ArrayList<>();
        //遍历日期集合
        for (String month : months) {
            month = month + ".31";//格式如:2019.10.31
            //调用MemberDao
            Integer memberCount = memberDao.findMemberCountBeforeDate(month);
            //将数据存储在集合中
            list.add(memberCount);
        }
        return list;
    }

    public List<Map<String, Integer>> findMemberSexList() {
        return memberDao.findMemberSexList();
    }

    public List<Map<String, Integer>> findMemberAgeList() {
        return memberDao.findMemberAgeList();
    }


}
